package com.happy.controller;

import com.happy.entity.CourseEntity;
import com.happy.entity.FileEntity;
import com.happy.service.ICourseService;
import com.happy.service.IFileService;
import com.happy.util.ConstUtil;
import com.happy.util.ResultBody;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: Wuzh
 * @Description:
 * @Date Created in 15:33 2021/1/23
 * @return
 */
@RestController
@RequestMapping ( "/course" )
@CrossOrigin ( value = "*", maxAge = 3600 )
public class CourseController {

    @Resource
    private ICourseService iCourseService;
    @Resource
    private IFileService iFileService;

    @RequestMapping ( "/queryList" )
    public ResultBody<CourseEntity> courseList ( CourseEntity courseEntity ) {
        List<CourseEntity> courseEntities = iCourseService.queryList ( courseEntity );
        return ResultBody.success ( courseEntities, courseEntities.size () );
    }

    /**
     * 保存视频
     * @param courseEntity
     * @return
     */
    @RequestMapping ( "saveCourse" )
    public ResultBody saveCourse ( @RequestBody CourseEntity courseEntity ) {
        courseEntity = iCourseService.saveCourse ( courseEntity );
        return ResultBody.success ( courseEntity );
    }


    /**
     * 上传笔记封面图片
     * 添加到FileEntity表并返回成功URL
     *
     * @param file
     * @return
     */
    @RequestMapping ( "uploadCover" )
    public ResultBody<FileEntity> uploadCover ( @RequestBody MultipartFile file, String courseId ) {
        FileEntity fileEntity = new FileEntity ();
        fileEntity.setType ( ConstUtil.FILE_TYPE_COURSE_COVER );
        fileEntity.setRelationId ( Integer.parseInt ( courseId ) );
        fileEntity = iFileService.addSingle ( fileEntity, file );
        return ResultBody.success ( fileEntity );
    }


}
